<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;
use Auth;
class SessionsController extends Controller
{


     public function __construct()
     {
       # code...
       $this->middleware('guest',[
         'only'=>['create']
       ]);
     }

    public function create()
    {
      # code...
      return view('sessions.create');
    }


    public function store(Request $request)
    {
      # code...
      $this->validate($request,[
        'email'=>'required|email|max:255',
        'password'=>'required'

      ]);

      $credentials=[
        'email'=>$request->email,
        'password'=>$request->password
      ];

      if(Auth::attempt($credentials,$request->has('remember'))){

        if (Auth::user()->activated){
             session()->flash('success','欢迎回来!');

            return redirect()->intended(route('users.show',[Auth::user()]));
          }else{

             Auth::logut();
            session()->flash('waring','你的账号未激活 请检查邮箱');
            return redirect('/');
        }


      }else{
        session()->flash('danger','很抱歉，你的邮箱和密码不正确');
        return redirect()->back();
      }


    }
    public function destroy()
    {
      Auth::logout();
      session()->flash('success','成功退出');
      return  redirect('login');

    }
}
